package com.wdy.golden.tinysql.util;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Dongye_Wu
 * @version: 1.0.0
 * @Description: TODO
 * @date 2020年4月18日 下午3:35:49
 */
public class ResultSetUtil {
	
	public static List<String> getResultColumns(ResultSet rs) throws SQLException{
		List<String> resultColumns = new ArrayList<String>();
		
		ResultSetMetaData rsmd = rs.getMetaData();
		for (int i = 1; i <= rsmd.getColumnCount(); i++) {
			resultColumns.add(rsmd.getColumnName(i));
		}
		
		return resultColumns;
	}
	
	public static List<String> getResultProperties(ResultSet rs) throws SQLException{
		List<String> resultProperties = new ArrayList<String>();
		
		ResultSetMetaData rsmd = rs.getMetaData();
		for (int i = 1; i <= rsmd.getColumnCount(); i++) {
			resultProperties.add(StringUtil.underlineLowerToCamel(rsmd.getColumnName(i)));
		}
		
		return resultProperties;
	}

    @SuppressWarnings("rawtypes")
    public static List<Map> rs2List(ResultSet rs) throws SQLException{
        List<Map> list = new ArrayList<>();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {
            Map<String,Object> rowData = new HashMap<>();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }
	
	public static Object getReturnObj(Class<?> typeClazz, ResultSet rs) throws SQLException {
		Object retObj = null;
		
		ResultSetMetaData rsmd = rs.getMetaData();

		List<Field> fieldList = new ArrayList<>();
		Class<?> clazz = typeClazz;
		while (clazz != null) {
			fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
			clazz = clazz.getSuperclass();
		}		
		
		try {
			retObj = typeClazz.newInstance();
			for (int i = 1; i <= rsmd.getColumnCount(); i++) {
				for (int j = 0; j < fieldList.size(); j++) {
					Field f = fieldList.get(j);
					if (StringUtil.underlineToCamelEquals(rsmd.getColumnName(i), f.getName())) {
						boolean flag = f.isAccessible();
						if (!flag) {
							f.setAccessible(true);
						}
						f.set(retObj, rs.getObject(i, f.getType()));
						f.setAccessible(flag);
						break;
					}
				}
			}
		} catch (InstantiationException | IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return retObj;
	}

	public static Object getReturnList(ResultSet rs, Class<?> typeClazz) throws SQLException {
		List<Object> retList = new ArrayList<Object>();
		
		while (rs.next()) {
			retList.add(getReturnObj(typeClazz, rs));
		}
		
		if(retList!=null && retList.size()>0) {
			return retList;
		}
		return null;
	}

	public static Object getReturnObject(ResultSet rs, Class<?> typeClazz) throws SQLException {
		while (rs.next()) {
			return getReturnObj(typeClazz, rs);
		}
		return null;
	}
}
